package net.zoneland.knowledge.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import net.zoneland.knowledge.enums.AttachTypeEnum;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.WorkApplyInfoAttachment;
import net.zoneland.knowledge.service.UniFlowService;
import net.zoneland.knowledge.service.WorkApplyInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public abstract class AbstractBaseApplyInfoController extends AbstractBaseController {
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractBaseApplyInfoController.class);

    /**
     * 任务id的参数名.
     */
    protected static final String TASK_ID_NAME = "taskId";
    /**
     * 工单id的参数名.
     */
    protected static final String WORK_APPLY_ID_NAME = "workApplyId";
    /**
     * 意见的参数名.
     */
    protected static final String OPINION_NAME = "opinion";
    /**
     * 任务id不能为空提示语.
     */
    protected static final String TASK_ID_BLANK_ERROR_MSG = "任务id不能为空！";

    /**
     * 工单不存在提示语.
     */
    protected static final String APPLY_NOT_EXIST = "当前工单不存在！id:";

    /**
     * 工单对象json的参数名.
     */
    protected static final String WORK_APPLY_INFO_JSON_NAME = "workApplyInfo";

    /**
     * 决策名称.
     */
    protected static final String DECISION_NAME = "decision";

    /**
     * json工具类.
     */
    @Autowired
    protected ObjectMapper objectMapper;

    /**
     * 流程业务接口.
     */
    @Autowired
    protected UniFlowService uniFlowService;

    /**
     * 工单处理业务接口.
     */
    @Autowired
    protected WorkApplyInfoService workApplyInfoService;

    /**
     * 获取taskId.
     * @param request http请求
     * @return taskId
     */
    protected String getAndCheckTaskId(final HttpServletRequest request) {
        final String taskId = request.getParameter(TASK_ID_NAME);
        Preconditions.checkArgument(StringUtils.isNotBlank(taskId), TASK_ID_BLANK_ERROR_MSG);
        return taskId;
    }

    /**
     * 获取workApplyId.
     * @param request http请求
     * @return workApplyId
     */
    protected String getAndCheckWorkApplyId(final HttpServletRequest request) {
        final String workApplyId = request.getParameter(WORK_APPLY_ID_NAME);
        Preconditions.checkArgument(StringUtils.isNotBlank(workApplyId), "工单信息id不能为空！");
        return workApplyId;
    }

    /**
     * 获取附件对象集合.
     * @param request http请求
     * @param type 类型
     * @param paramName 参数名
     * @param user 用户对象
     * @return 附件对象集合
     */
    protected List<WorkApplyInfoAttachment> getAttachments(final MultipartHttpServletRequest request,
                                                         final AttachTypeEnum type,
                                                         final String paramName, final LoginUserPojo user) {
        List<MultipartFile> fileRes = request.getFiles(paramName);
        if (CollectionUtils.isNotEmpty(fileRes)) {
            return fileRes.stream().filter(it -> !it.isEmpty()).map(it -> {
                try {
                    LOGGER.info("fileName:{}, size:{}", it.getOriginalFilename(), it.getSize());
                    WorkApplyInfoAttachment attachment = new WorkApplyInfoAttachment();
                    attachment.setAttachment(it.getBytes());
                    attachment.setUploadTime(DateTime.now().toDate());
                    attachment.setType(type);
                    attachment.setUploaderUid(user.getUserID());
                    attachment.setUploaderName(user.getUserName());
                    attachment.setFileName(it.getOriginalFilename());
                    return attachment;
                } catch (IOException e) {
                    LOGGER.error("获取文件流失败，文件名称:{}", it.getOriginalFilename(), e);
                }
                return null;
            }).filter(Objects::nonNull).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }
}
